package com.kitty.game.rank.service.handler;

import com.kitty.game.role.model.Role;
import com.kitty.common.utils.SpringUtils;
import com.kitty.game.enter.FiedValue;
import com.kitty.game.rank.model.PersonInfo;
import com.kitty.game.rank.message.RespRankTopUser;
import com.kitty.game.rank.model.Range;
import com.kitty.game.rank.model.RankType;
import com.kitty.game.rank.model.pojo.DataUnit;
import com.kitty.game.rank.model.pojo.RankGroupListData;
import com.kitty.game.rank.model.record.RankRecord;
import com.kitty.game.rank.model.record.YingXiongRankRecord;
import lombok.extern.slf4j.Slf4j;
import org.nutz.dao.Dao;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.util.ArrayList;
import java.util.List;

/**英雄排行榜处理类*/
@Component
@Slf4j
public class YingXiongRankHandler extends RangeRankHandler {
    private List<Range<Short>> levelRanges = new ArrayList<>();

    /**初始化等级段*/
    @PostConstruct
    private void initRange() {
        levelRanges.add(new Range((short) 70, (short) 79));
        levelRanges.add(new Range((short) 80, (short) 89));
        levelRanges.add(new Range((short) 90, (short) 99));
        levelRanges.add(new Range((short) 100, (short) 109));
        levelRanges.add(new Range((short) 110, (short) 119));
        levelRanges.add(new Range((short) 120, (short) 139));
    }

    @Override
    public void init() {
        Dao dao = SpringUtils.getBean(Dao.class);
        RankGroupListData<YingXiongRankRecord> rankGroupListData = dao.fetch(RankGroupListData.class, RankType.YING_XIONG_HUI.name());
        if (rankGroupListData != null) {
            rankGroupListData.doAfterInit(YingXiongRankRecord.class);

            List<DataUnit> rankData = rankGroupListData.getRankData();
            for (DataUnit dataUnit : rankData) {
                String levelRange = dataUnit.getKey();
                for (RankRecord record : dataUnit.getRecords()) {
                    YingXiongRankRecord rankRecord = (YingXiongRankRecord) record;
                    update(levelRange, rankRecord.getGid(), record);
                }
            }
        }
    }

    public void save() {
        List<DataUnit> rankData = new ArrayList<>();
        for (Range<Short> range : levelRanges) {
            String levelRange = getLevelRange(range);
            List<RankRecord> records = getRecordsFrom(levelRange, 0, getRankSize());
            if (records == null || records.size() == 0) {continue ;}

            DataUnit dataUnit = new DataUnit(levelRange, records);
            rankData.add(dataUnit);
        }

        if (rankData.size() <= 0) {return ;}

        RankGroupListData<YingXiongRankRecord> rankGroupListData = new RankGroupListData<>(RankType.YING_XIONG_HUI, rankData);
        rankGroupListData.doBeforeSave();
        Dao dao = SpringUtils.getBean(Dao.class);
        dao.insertOrUpdate(rankGroupListData);
    }

    @PreDestroy
    private void shutDown() {
        save();
        log.error("[{}]保存数据后关闭", this.getClass().getSimpleName());
    }

    private String getLevelRange(Range range) {
        return range.getMin() + "-" + range.getMax();
    }

    /**找到角色对应的分段*/
    public String getLevelRange(Role role) {
        short roleLevel = role.getLevel();
        Range<Short> range = getRange(roleLevel);
        if (range != null) {
            return getLevelRange(range);
        }
        return null;
    }

    private Range getRange(short roleLevel) {
        for (Range<Short> range : levelRanges) {
            if (roleLevel >= range.getMin() && roleLevel <= range.getMax()) {
                return range;
            }
        }

        return null;
    }

    public RespRankTopUser getRankResponse(short type, String range) {
        RespRankTopUser respRankTopUser = new RespRankTopUser();
        respRankTopUser.setType(type);
        respRankTopUser.setCookie(new Long(System.currentTimeMillis() / 1000).intValue());
        respRankTopUser.setList(new ArrayList<>());
        List<PersonInfo> personInfoList = getPersonInfoList(getRecordsFrom(range));
        respRankTopUser.setList(personInfoList);
        respRankTopUser.setSize((short) respRankTopUser.getList().size());
        String[] levels = range.split("-");
        respRankTopUser.setMin(Short.parseShort(levels[0]));
        respRankTopUser.setMax(Short.parseShort(levels[1]));
        return respRankTopUser;
    }

    public List<RankRecord> getRecordsFrom(String range) {
        return getRecordsFrom(range, 0, getRankSize());
    }

    private List<PersonInfo> getPersonInfoList(List<RankRecord> records) {
        List<PersonInfo> list = new ArrayList<>(records.size());
        for (RankRecord rankRecord : records) {
            YingXiongRankRecord yingXiongRankRecord = (YingXiongRankRecord)rankRecord;
            PersonInfo personInfo = new PersonInfo();
            personInfo.setList(new ArrayList<>());
            personInfo.getList().add(new FiedValue(305, yingXiongRankRecord.getGid()));
            personInfo.getList().add(new FiedValue(1, yingXiongRankRecord.getName()));
            personInfo.getList().add(new FiedValue(855, yingXiongRankRecord.getPartyName()));
            personInfo.getList().add(new FiedValue(938, yingXiongRankRecord.getTime()));
            personInfo.getList().add(new FiedValue(31, yingXiongRankRecord.getLevel()));
            list.add(personInfo);
        }
        return list;
    }

    public void update(Role role, int time) {
        Range range = getRange(role.getLevel());
        if (range == null) {return ;}

        String levelRange = getLevelRange(range);
        this.update(levelRange, role.getGid(), new YingXiongRankRecord(role.getGid(), role.getName(), role.getPartyName(), time, role.getLevel()));
    }

    public void update(String gid, short level, String partyName, String name, int time) {
        Range range = getRange(level);
        if (range == null) {return ;}

        String levelRange = getLevelRange(range);
        this.update(levelRange, gid, new YingXiongRankRecord(gid, name, partyName, time, level));
    }

    /**角色升级跨等级段处理*/
    public void handleRoleUpgrade(Role role, short oldLevel, short newLevel) {
        /**判断角色是否跨等级段，是的话则从旧等级段排行榜删除，加入新等级段排行榜*/

        Range oldRange = getRange(oldLevel);
        Range newRange = getRange(newLevel);
        /**同一等级段时，返回*/
        if (oldRange != null && oldRange.equals(newRange)) {return ;}

        if (oldRange != null) {
            String oldLevelRange = getLevelRange(oldRange);

            YingXiongRankRecord rankRecord = (YingXiongRankRecord)remove(oldLevelRange, role.getGid());
            if (rankRecord != null) {
                String newLevelRange = getLevelRange(newRange);
                this.update(newLevelRange, role.getGid(), new YingXiongRankRecord(role.getGid(), role.getName(), role.getPartyName(), rankRecord.getTime(), role.getLevel()));
            }
        }
    }
}
